﻿using DevelopTool.Common;
using DevelopTool.Model.DbModels.TableRelation;
using ST.Library.UI.NodeEditor;
using System.Collections.Generic;
using System.Drawing;

namespace DevelopTool.Forms.DbForms.TableRelationForms
{
    [STNode("/")]
    public class TableNode : STNode
    {
        private TableRelationNode _node;

        public TableNode()
        {
        }

        public TableNode(TableRelationNode node)
        {
            _node = node;
            this.TitleColor = Setting.Theme.BackColor;//节点标题背景色
            this.ForeColor = Setting.Theme.ForeColor;//节点标题文本色
            Load();
        }


        public TableNode(string title,List<string> inColumns,List<string> outColumn,Rectangle rectangle)
        {
            _node = new TableRelationNode();
            _node.Title = title;
            _node.InputOptions = inColumns;
            _node.OutputOptions = outColumn;
            _node.Rectangle = rectangle;
            Load();
        }

        private void Load()
        {
            this.Title = _node.Title;
            this.LetGetOptions = true;

            for(int i = 0 ; i < _node.InputOptions.Count ; i++)
            {
                string inColumnStr = _node.InputOptions[i];
                string outColumnStr = _node.OutputOptions[i];
                if(string.IsNullOrEmpty(inColumnStr)) continue;

                STNodeOption inputOption = new STNodeOption($"{inColumnStr}",typeof(string),false);

                if(!string.IsNullOrEmpty(outColumnStr) && outColumnStr.Length > 20)
                {
                    outColumnStr = outColumnStr.Substring(0,20);
                }

                STNodeOption outputOption = new STNodeOption($"{outColumnStr}",typeof(string),false);



                inputOption.Data = inColumnStr;
                outputOption.Data = inColumnStr;

                this.InputOptions.Add(inputOption);
                this.OutputOptions.Add(outputOption);
            }

            if(_node.Rectangle.Left > 0 && _node.Rectangle.Top > 0)
            {
                this.Left = _node.Rectangle.Left;
                this.Top = _node.Rectangle.Top;
            }

        }

    }
}
